package com.getpebble.android.framework.endpoint;

import android.content.Context;
import com.getpebble.android.bluetooth.protocol.ProtocolMessage;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.model.FrameworkState;
import com.getpebble.android.framework.datalogging.Datalogging;
import com.getpebble.android.framework.endpoint.EndpointRequest.EndpointAction;
import com.getpebble.android.framework.endpoint.EndpointRequest.EndpointArgumentKeys;
import com.getpebble.android.framework.protocol.EndpointId;
import com.getpebble.android.framework.protocol.inbound.PblInboundDataloggingMessage;
import com.getpebble.android.framework.protocol.outbound.PblOutboundDataloggingMessage;
import com.google.common.collect.ImmutableSet;
import com.google.common.primitives.UnsignedInteger;
import com.sun.mail.imap.protocol.ListInfo;
import com.sun.mail.smtp.SMTPMessage;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public class DataloggingEndpoint extends RequestableEndpoint {
    private static final String TAG = DataloggingEndpoint.class.getSimpleName();
    protected Datalogging mDatalogging;
    private final IEndpointMessageSender mMessageSender;

    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$framework$endpoint$EndpointRequest$EndpointAction = new int[EndpointAction.values().length];

        static {
            try {
                $SwitchMap$com$getpebble$android$framework$endpoint$EndpointRequest$EndpointAction[EndpointAction.SEND_DATALOGGING_ACK.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$endpoint$EndpointRequest$EndpointAction[EndpointAction.SEND_DATALOGGING_NACK.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$endpoint$EndpointRequest$EndpointAction[EndpointAction.SEND_DATALOGGING_REPORT_OPEN_SESSIONS.ordinal()] = 3;
            } catch (NoSuchFieldError e3) {
            }
        }
    }

    public DataloggingEndpoint(Context context, IEndpointMessageSender messageSender) {
        if (context == null) {
            throw new IllegalArgumentException("context cannot be null");
        } else if (messageSender == null) {
            throw new IllegalArgumentException("messageSender cannot be null");
        } else {
            this.mDatalogging = Datalogging.getInstance(context);
            this.mMessageSender = messageSender;
        }
    }

    boolean onRequest(EndpointRequest request, FrameworkState frameworkState) {
        Trace.debug(TAG, "onRequest action = " + request.getAction());
        switch (AnonymousClass1.$SwitchMap$com$getpebble$android$framework$endpoint$EndpointRequest$EndpointAction[request.getAction().ordinal()]) {
            case SMTPMessage.RETURN_FULL /*1*/:
                this.mMessageSender.sendMessage(PblOutboundDataloggingMessage.createAckMessage(UnsignedInteger.valueOf((long) request.getInt(EndpointArgumentKeys.DATALOGGING_SESSION).intValue())));
                return true;
            case SMTPMessage.RETURN_HDRS /*2*/:
                this.mMessageSender.sendMessage(PblOutboundDataloggingMessage.createNackMessage(UnsignedInteger.valueOf((long) request.getInt(EndpointArgumentKeys.DATALOGGING_SESSION).intValue())));
                return true;
            case ListInfo.INDETERMINATE /*3*/:
                int[] ints = request.getIntArray(EndpointArgumentKeys.DATALOGGING_SESSIONS);
                List<UnsignedInteger> sessionIds = new LinkedList();
                for (int i : ints) {
                    sessionIds.add(UnsignedInteger.valueOf((long) i));
                }
                this.mMessageSender.sendMessage(PblOutboundDataloggingMessage.createReportOpenSessionsMessage(sessionIds));
                return true;
            default:
                Trace.debug(TAG, "No matching request found in DataloggingEndpoint, not handling.");
                return false;
        }
    }

    boolean onPrfRequest(EndpointRequest request, FrameworkState frameworkState) {
        return true;
    }

    Set<EndpointId> getSupportedEndpoints() {
        return ImmutableSet.of(EndpointId.DATA_LOG);
    }

    boolean onReceive(ProtocolMessage message) {
        if (message == null) {
            Trace.error(TAG, "Received null message, dropping");
            return false;
        }
        this.mDatalogging.handleWatchMessage(new PblInboundDataloggingMessage(message));
        return true;
    }
}
